/**
 * PDF优化导出功能
 * 专门为PDF导出创建全宽度布局
 */

export const usePdfOptimizedExport = () => {
  /**
   * 创建PDF优化的HTML内容
   * @param element 要导出的DOM元素
   * @param filename 文件名
   */
  const createPdfOptimizedContent = (element: HTMLElement, filename: string = 'resume.pdf') => {
    // 获取元素内容
    const elementContent = element.innerHTML
    
    // 创建PDF优化的HTML内容
    const pdfContent = `
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <title>${filename}</title>
        <script src="https://cdn.tailwindcss.com"></script>
        <style>
          /* PDF专用样式 */
          body {
            font-family: 'Microsoft YaHei', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
            line-height: 1.6;
            color: #1f2937;
            margin: 0;
            padding: 0;
            background: white;
          }
          
          .pdf-container {
            width: 100% !important;
            max-width: 100% !important;
            margin: 0 !important;
            padding: 20px !important;
            background: white;
            font-family: 'Microsoft YaHei', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
            line-height: 1.6;
          }
          
          /* 确保所有Tailwind类在PDF中正确工作 */
          .flex { display: flex !important; }
          .flex-col { flex-direction: column !important; }
          .items-start { align-items: flex-start !important; }
          .items-center { align-items: center !important; }
          .justify-between { justify-content: space-between !important; }
          .flex-1 { flex: 1 1 0% !important; }
          .ml-auto { margin-left: auto !important; }
          
          .text-3xl { font-size: 1.875rem !important; line-height: 2.25rem !important; }
          .text-lg { font-size: 1.125rem !important; line-height: 1.75rem !important; }
          .text-base { font-size: 1rem !important; line-height: 1.5rem !important; }
          .text-sm { font-size: 0.875rem !important; line-height: 1.25rem !important; }
          
          .font-bold { font-weight: 700 !important; }
          .font-semibold { font-weight: 600 !important; }
          .font-medium { font-weight: 500 !important; }
          
          .text-blue-500 { color: #3b82f6 !important; }
          .text-gray-800 { color: #1f2937 !important; }
          .text-gray-600 { color: #4b5563 !important; }
          .text-gray-500 { color: #6b7280 !important; }
          
          .border-b-2 { border-bottom-width: 2px !important; }
          .border-b { border-bottom-width: 1px !important; }
          .border-blue-500 { border-color: #3b82f6 !important; }
          .border-gray-200 { border-color: #e5e7eb !important; }
          
          .mb-8 { margin-bottom: 2rem !important; }
          .mb-5 { margin-bottom: 1.25rem !important; }
          .mb-3 { margin-bottom: 0.75rem !important; }
          .mb-2 { margin-bottom: 0.5rem !important; }
          .pb-5 { padding-bottom: 1.25rem !important; }
          .pb-2 { padding-bottom: 0.5rem !important; }
          .gap-3 { gap: 0.75rem !important; }
          .gap-1 { gap: 0.25rem !important; }
          
          .leading-relaxed { line-height: 1.625 !important; }
          .m-0 { margin: 0 !important; }
          .p-0 { padding: 0 !important; }
          
          /* 打印优化 */
          @media print {
            body {
              margin: 0;
              padding: 0;
              background: white;
            }
            
            .pdf-container {
              width: 100% !important;
              max-width: 100% !important;
              margin: 0 !important;
              padding: 15px !important;
              box-shadow: none !important;
            }
          }
        </style>
      </head>
      <body>
        <div class="pdf-container">
          ${elementContent}
        </div>
        <script>
          window.onload = function() {
            setTimeout(() => {
              window.print();
              window.onafterprint = function() {
                window.close();
              };
            }, 500);
          };
        </script>
      </body>
      </html>
    `
    
    return pdfContent
  }

  /**
   * 使用优化的打印功能导出PDF
   * @param element 要导出的DOM元素
   * @param filename 文件名
   */
  const exportToPdfOptimized = (element: HTMLElement, filename: string = 'resume.pdf') => {
    try {
      // 创建新窗口
      const printWindow = window.open('', '_blank')
      if (!printWindow) {
        throw new Error('无法打开打印窗口，请检查浏览器弹窗设置')
      }

      // 创建PDF优化的内容
      const printContent = createPdfOptimizedContent(element, filename)

      // 写入内容到新窗口
      printWindow.document.write(printContent)
      printWindow.document.close()

      return true
    } catch (error) {
      console.error('优化PDF导出失败:', error)
      throw error
    }
  }

  return {
    exportToPdfOptimized,
    createPdfOptimizedContent
  }
}
